package com.dalimao.floateutil.widget;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;

public class ShapeView extends Component implements Component.DrawTask, Component.EstimateSizeListener {

    private ShapeEnum mCurrentShape = ShapeEnum.Circular;
    private Paint mCirclePaint, mTrianglePaint, mSquarePaint;
    private Path mPath;

    public ShapeView(Context context) {
        this(context, null);
    }

    public ShapeView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    public ShapeView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mCirclePaint = initPaintByColor(Color.RED);
        mTrianglePaint = initPaintByColor(Color.YELLOW);
        mSquarePaint = initPaintByColor(Color.GRAY);
    }

    private Paint initPaintByColor(Color color) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(color);
        return paint;
    }

    public enum ShapeEnum{
        Circular,Square,Triangle
    }


    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
        int width = EstimateSpec.getSize(widthMeasureSpec);
        int height = EstimateSpec.getSize(heightMeasureSpec);
        setEstimatedSize(Math.min(width, height), Math.min(width, height));
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        switch (mCurrentShape) {
            case Circular:
                // 画圆形
                int center = getWidth() / 2;
                canvas.drawCircle(center, center, center, mCirclePaint);
                break;
            case Square:
                // 画正方形
                canvas.drawRect(0, 0, getWidth(), getHeight(), mSquarePaint);
                break;
            case Triangle:
                // 画三角  Path 画路线
                if (mPath == null) {
                    mPath = new Path();
                    mPath.moveTo(getWidth() >> 1, 0);
                    //勾股定理
                    mPath.lineTo(0, (float) (Math.sqrt(3) * getWidth() / 2));
                    mPath.lineTo(getWidth(), ((float) Math.sqrt(3) * getWidth() / 2));
                    mPath.close();//直接连接到起点
                }
                canvas.drawPath(mPath, mTrianglePaint);
                break;
        }
    }

    public void exchange() {
        switch (mCurrentShape) {
            case Circular:
                mCurrentShape = ShapeEnum.Square;
                break;
            case Square:
                mCurrentShape = ShapeEnum.Triangle;
                break;
            case Triangle:
                mCurrentShape = ShapeEnum.Circular;
                break;
        }
        // 不断重新绘制形状
        invalidate();
    }

    public ShapeEnum getCurrentShape() {
        return mCurrentShape;
    }
}